﻿namespace CodeParser.Tokens
{
    /// <summary>
    /// Detects first matching keyword
    /// </summary>
    public class Keywords
        : TokenParser
    {

        public Keywords(TokenType type, params string[] keywords)
            : base(type)
        {
            KeywordList = keywords;
        }

        public override Token GetTokenAt(string source, int index)
        {
            for (var i = 0; i < _keywordCount; i++)
            {
                var keyword = KeywordList[i];
                if (StringUtils.Matches(source, index, keyword)) return new Token() { Index = index, Type = Type.Value, Value = source.Substring(index, keyword.Length) };
            }
            return null;
        }

        private int _keywordCount;
        private string[] _keywords;
        public string[] KeywordList
        {
            get { return _keywords; }
            private set
            {
                _keywords = value;
                _keywordCount = value.Length;
            }
        }

    }
}